function PagePlugin(container,options) {
    //页码按钮容器
    this.container=container;
    //单页也显示的信息数量，默认为5
    this.showMsgCount=options.showMsgCount || 5;
    //所有的信息数量
    this.allPageCount=Math.ceil(options.allMsgCount/options.showMsgCount);
    //可视的页码
    this.showPageCount=options.showPageCount*1>this.allPageCount*1?this.allPageCount:options.showPageCount;
    //按钮激活的样式
    this.pageBtnStyle=options.pageBtnStyle;
    this.pageBtnArr=[];//储存所有页码按钮的数组
    this.currentPage=1;//当前响应的页码
    this.firstPageBtn=null;
    this.prevPageBtn=null;
    this.nextPageBtn=null;
    this.lastPageBtn=null;
    this.backFn=options.backFn;
    this.init();
}
PagePlugin.prototype={
    init:function () {
        //当前函数作用域对象赋值给that
        var that=this;
        //首页按钮
        this.firstPageBtn=this.createBtn("li","首页","firstPageBtn","hidden");
        this.container.appendChild(this.firstPageBtn);
        this.firstPageBtn.addEventListener("click",function (e) {
            if(!e){
                e=window.event;
            }
            that.currentPage=1;
            that.changePageStyle();
            that.backFn(that.currentPage,that.showMsgCount);
        });
        //上一页
        this.prevPageBtn=this.createBtn("li","上一页","prevPageBtn","hidden");
        this.container.appendChild(this.prevPageBtn);
        this.prevPageBtn.addEventListener("click",function (e) {
            if(!e){
                e=window.event;
            }
            if(that.currentPage==1){
                return;
            }
            that.currentPage--;
            that.changePageStyle();
            that.backFn(that.currentPage,that.showMsgCount);
        });
        //页码按钮
        for(var i=1;i<=this.showPageCount;i++){
            var pageBtn=this.createBtn("li",i,"pageBtn");
            this.container.appendChild(pageBtn);
            this.pageBtnArr.push(pageBtn);
            if(i==1){
                pageBtn.className="pageBtn "+this.pageBtnStyle;
            }
            pageBtn.addEventListener("click",function (e) {
                if(!e){
                    e=window.event;
                }
                that.currentPage=this.textContent*1;
                that.changePageStyle();
                that.backFn(that.currentPage,that.showMsgCount);
            });
        }
        //下一页
        this.nextPageBtn=this.createBtn("li","下一页","nextPageBtn");
        this.container.appendChild(this.nextPageBtn);
        this.nextPageBtn.addEventListener("click",function (e) {
            if(!e){
                e=window.event;
            }
            if(that.currentPage==that.allPageCount){
                return;
            }
            that.currentPage++;
            that.changePageStyle();
            that.backFn(that.currentPage,that.showMsgCount);
        });
        //尾页按钮
        this.lastPageBtn=this.createBtn("li","尾页","lastPageBtn","hidden");
        this.container.appendChild(this.lastPageBtn);
        this.lastPageBtn.addEventListener("click",function (e) {
            if(!e){
                e=window.event;
            }
            that.currentPage=that.allPageCount;
            that.changePageStyle();
            that.backFn(that.currentPage,that.showMsgCount);
        });
        this.backFn(this.currentPage,this.showMsgCount);
    },
    createBtn:function (elem,text,className,type) {
        var btn=document.createElement(elem);
        btn.textContent=text;
        btn.className=className;
        btn.style.visibility=type;
        return btn;
    },
    changePageStyle:function () {
        //首页状态
        if(this.currentPage==1){
            this.showHide("hidden","hidden","visible","hidden");
            for(var j=0;j<this.pageBtnArr.length;j++){
                this.pageBtnArr[j].textContent=j+1;
            }
        }
        //上一页状态
        else if(this.currentPage<=this.showPageCount/2){
            this.showHide("hidden","visible","visible","hidden");
            for(var k=0;k<this.pageBtnArr.length;k++){
                this.pageBtnArr[k].textContent=k+1;
            }
        }
        //尾页消失
        else if(this.currentPage>=this.allPageCount-this.showPageCount/2-1){
            this.showHide("visible","visible","visible","hidden");
            for(var s=0;s<this.pageBtnArr.length;s++){
                this.pageBtnArr[s].textContent=this.allPageCount-this.showPageCount+1+s;
            }
        }

        //全部显示
        else if(this.currentPage>this.showPageCount){
            this.showHide("visible","visible","visible","visible");
            for(var m=0;m<this.pageBtnArr.length;m++){
                this.pageBtnArr[m].textContent=this.currentPage-Math.floor(this.showPageCount/2)+1+m;
            }
        }
        //上一页首页显示
        else if(this.currentPage>this.showPageCount/2){
            this.showHide("visible","visible","visible","hidden");
            for(var n=0;n<this.pageBtnArr.length;n++){
                this.pageBtnArr[n].textContent=this.currentPage-Math.floor(this.showPageCount/2)+1+n;
            }
        }
        //下一页消失
        if(this.currentPage==this.allPageCount){
            this.showHide("visible","visible","hidden","hidden");
        }
        //遍历所有的页码按钮
        for(var i=0;i<this.pageBtnArr.length;i++){
            if(this.pageBtnArr[i].textContent==this.currentPage){
                this.pageBtnArr[i].className="pageBtn "+this.pageBtnStyle;
            }else {
                this.pageBtnArr[i].className="pageBtn";
            }
        }

    },
    showHide:function (type1,type2,type3,type4) {
        this.firstPageBtn.style.visibility=type1;
        this.prevPageBtn.style.visibility=type2;
        this.nextPageBtn.style.visibility=type3;
        this.lastPageBtn.style.visibility=type4;
    }
};
PagePlugin.prototype.constructor=PagePlugin;